package com.yhk.wuchat.common.common.event.listener;

import com.yhk.wuchat.common.common.event.UserRegisterEvent;
import com.yhk.wuchat.common.user.dao.UserDao;
import com.yhk.wuchat.common.user.domain.entity.User;
import com.yhk.wuchat.common.user.domain.enums.IdempotentEnum;
import com.yhk.wuchat.common.user.domain.enums.ItemEnum;
import com.yhk.wuchat.common.user.service.IUserBackpackService;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/4/16 21:17
 * @注释
 */
@Component
public class UserRegisterListener {

    @Resource
    private IUserBackpackService userBackpackService;
    @Resource
    private UserDao userDao;
    @TransactionalEventListener(classes = UserRegisterEvent.class,phase = TransactionPhase.AFTER_COMMIT)
    @Async
    public void sendCard(UserRegisterEvent event){
        User user = event.getUser();
        userBackpackService.acquireItem(user.getId(), ItemEnum.MODIFY_NAME_CARD.getId(), IdempotentEnum.UID,user.getId().toString());
    }

    @TransactionalEventListener(classes = UserRegisterEvent.class,phase = TransactionPhase.AFTER_COMMIT)
    @Async
    public void sendBadge(UserRegisterEvent event){
        User user = event.getUser();
        int count = userDao.count();
        if(count<10){
            userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID,user.getId().toString());
        }else if(count<100){
            userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP100_BADGE.getId(), IdempotentEnum.UID,user.getId().toString());
        }

    }
}
